package com.xuecheng.manager_cms_client.service;

import com.mongodb.client.gridfs.GridFSBucket;
import com.mongodb.client.gridfs.GridFSDownloadStream;
import com.mongodb.client.gridfs.model.GridFSFile;
import com.xuecheng.framework.domain.cms.CmsPage;
import com.xuecheng.framework.domain.cms.CmsSite;
import com.xuecheng.framework.exception.ExceptionCast;
import com.xuecheng.framework.model.response.CommonCode;
import com.xuecheng.manager_cms_client.dao.CmsPageRepository;
import com.xuecheng.manager_cms_client.dao.CmsSiteRepository;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.gridfs.GridFsResource;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.Optional;

@Service
public class PageService {

    private static final Logger logger = LoggerFactory.getLogger(PageService.class);
    @Autowired
    private CmsPageRepository pageRepository;
    @Autowired
    private CmsSiteRepository siteRepository;
    @Autowired
    private GridFsTemplate gridFsTemplate;

    @Autowired
    private GridFSBucket gridFSBuckets;

    public void savePageToServerPath(String pageId){
        Optional<CmsPage> optional = pageRepository.findById(pageId);
        if (!optional.isPresent()){
            logger.info("receive cms post page,cmsPage is null:{}","pageId:"+pageId);
            ExceptionCast.cast(CommonCode.INVALID_PARAM);
        }
        CmsPage cmsPage = optional.get();
        String pagePhysicalPath = cmsPage.getPagePhysicalPath();
        String pageName = cmsPage.getPageName();

        String siteId = cmsPage.getSiteId();
        Optional<CmsSite> optionalCmsSite = siteRepository.findById(siteId);
        if (!optionalCmsSite.isPresent()){
            ExceptionCast.cast(CommonCode.INVALID_PARAM);
        }
        CmsSite cmsSite = optionalCmsSite.get();
        //todo 这里因该是站点物理id，但是没有
        String siteWebPath = cmsSite.getSiteWebPath();

        String path = siteWebPath + pagePhysicalPath + pageName;
        String htmlFileId = cmsPage.getHtmlFileId();
        InputStream inputStreamHtmlFile = this.getHtmlFileByFileId(htmlFileId);
        try {
            FileOutputStream fos=new FileOutputStream(new File(path));
            IOUtils.copy(inputStreamHtmlFile,fos);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private InputStream getHtmlFileByFileId(String htmlFileId) {
        GridFSFile gridFSFile = gridFsTemplate.findOne(Query.query(Criteria.where("_id").is(htmlFileId)));
        GridFSDownloadStream downloadStream = gridFSBuckets.openDownloadStream(gridFSFile.getObjectId());
        GridFsResource gridFsResource = new GridFsResource(gridFSFile,downloadStream);

        try {
            return gridFsResource.getInputStream();

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;

    }
}
